//
//  HJStatusRetweetView.swift
//  Micro-blog Sina
//
//  Created by admin on 16/3/27.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit
import SnapKit

class HJStatusRetweetView: UIView {
    // MARK: - 属性
    var retweetViewBottomCons: Constraint?
    
    var retweetStatus: HJStatuses? {
        didSet {
            guard let screen_name = retweetStatus?.user?.screen_name else {
                HJPrint(msg: "retweetStatus?.user?.screen_name 没有值")
                return
            }
            guard let text = retweetStatus?.text else {
                HJPrint(msg: "retweetStatus?.text 没有值")
                return
            }
            retweetContentLabel.text = "@\(screen_name): \(text)"
            
            retweetViewBottomCons?.uninstall()
            if let pictureURLs = retweetStatus?.storePictureURLs {
                retweetPictureView.hidden = false
                retweetPictureView.pictureURLs = pictureURLs
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomCons = make.bottom.equalTo(retweetPictureView).offset(HJStatusCellMargin).constraint
                })
            }else {
                retweetPictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomCons = make.bottom.equalTo(retweetContentLabel).offset(HJStatusCellMargin).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    private func prepareUI() {
        addSubview(backgroundButton)
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        
        backgroundButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(HJStatusCellMargin)
            make.trailing.equalTo(self).offset(-HJStatusCellMargin)
        }
        
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetContentLabel)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(HJStatusCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBottomCons = make.bottom.equalTo(retweetPictureView).offset(HJStatusCellMargin).constraint
        }
    }
    
    // MARK: - Lazy load
    private lazy var backgroundButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
    private lazy var retweetContentLabel: UILabel = {
       let label = UILabel()
        label.font = UIFont.systemFontOfSize(HJStatusCellContentSize)
        label.numberOfLines = 0
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
    private lazy var retweetPictureView: HJStatusPictureView = HJStatusPictureView()
}
